//
//  CodeLoginVC.m
//  jinwang
//
//  Created by jzb on 2018/3/9.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "CodeLoginVC.h"

#define KTime   60//设置重新发送的时间  自己可以改

@interface CodeLoginVC ()
{
    
    NSTimer *_myTimer;//定时器
}
@property (weak, nonatomic) IBOutlet UITextField *PhoneTF;
@property (weak, nonatomic) IBOutlet UITextField *CodeTF;
@property (nonatomic,assign) NSInteger timer;

@end

@implementation CodeLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"验证码登录";
    _timer = KTime;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([textField isEqual:self.PhoneTF]) {
        NSUInteger length = textField.text.length + string.length - range.length;
        if (length <= 11) {
            return YES;
        }else{
            [self.PhoneTF shakeWithShakeDirection:ShakingDirectionHorizontal];
            return NO;
        }
    }else if ([textField isEqual:self.CodeTF]) {
        NSUInteger length = textField.text.length + string.length - range.length;
        if (length <= 6) {
            return YES;
        }else{
            [self.CodeTF shakeWithShakeDirection:ShakingDirectionHorizontal];
            return NO;
        }
    }else{
        return YES;
    }
}

- (IBAction)CodeBtnOnclick:(UIButton *)sender {
                
    if (kStringIsEmpty(self.PhoneTF.text)) {
        [self showBottomMessag:@"请输入手机号"];
        return;
    }
    else if (![NSString IsPhoneNumber:self.PhoneTF.text]) {
        [self showBottomMessag:@"请输入正确的手机号"];
        return;
    }
    else{
        [self getCode:self.PhoneTF.text currentBtn:sender];
    }
}
- (IBAction)LoginBtnOnclick:(UIButton *)sender {
    
    //登录
    if (kStringIsEmpty(self.PhoneTF.text)) {
        [self showBottomMessag:@"请输入手机号"];
        return;
    }else if (![NSString IsPhoneNumber:self.PhoneTF.text]) {
        [self showBottomMessag:@"请输入正确的手机号"];
        return;
    }else if (self.CodeTF.text.length < 6) {
        [self showCenterMessage:@"验证码长度为六位"];
        return;
    }
    
    [[DDNetworkManagerDate makeUrl_verifylogin_with_user_phone:self.PhoneTF.text code:self.CodeTF.text] post_RequestFinshSuccess:^(id responseObject) {
            NSLog(@"-=-=-=-=>>>%@",responseObject);
            if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {                
                
                __weak typeof(self) weakSelf = self;
                [[EMClient sharedClient] loginWithUsername:self.PhoneTF.text
                                                  password:kEMPassword
                                                completion:^(NSString *aUsername, EMError *aError) {
                                                    if (!aError) {
                                                        NSLog(@"登录成功");
                                                        
                                                        [kUserDefaults setObject:self.PhoneTF.text forKey:@"user_phone"];
                                                        NSDictionary *data = [responseObject objectForKey:kRequsetDataKey];
                                                        [kUserDefaults setObject:data[@"user_token"] forKey:@"user_token"];
                                                        [self net_getUserInfo];
                                                        MainTabBarController *tabBarC = [[MainTabBarController alloc]init];
                                                        kKeyWindow.rootViewController = tabBarC;
                                                        
                                                    } else {
                                                        NSLog(@"登录失败");
                                                    }
                                                }];

                
            }
        } failure:^(id errorObject) {
        }];
    
}

#pragma mark -- 获取验证码
- (void)getCode:(NSString *)text currentBtn:(UIButton *)currentBtn
{
    [[DDNetworkManagerDate makeUrl_sendsms_with_user_phone:text type:@"0"] post_RequestFinshSuccess:^(id responseObject) {
        NSLog(@"====>>>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            
            NSDictionary *dic = responseObject;
            
            NSLog(@"====>>>%@",dic);
            currentBtn.enabled = NO;
            [currentBtn setTitle:[NSString stringWithFormat:@"剩余%zis", _timer] forState:UIControlStateNormal];
            _myTimer = [NSTimer scheduledTimerWithTimeInterval:1 block:^(NSTimer * _Nonnull timer) {
                [currentBtn setTitle:[NSString stringWithFormat:@"剩余%zis", _timer] forState:UIControlStateNormal];
                if (_timer == 0) {
                    [currentBtn setTitle:@"重新获取" forState:UIControlStateNormal];
                    currentBtn.enabled = YES;
                    [_myTimer invalidate];
                    _myTimer = nil;
                    _timer = KTime;
                }else{
                    _timer --;
                }
            } repeats:YES];
        }
    } failure:^(id errorObject) {
    }];
}
//获取用户信息
-(void)net_getUserInfo{
    [[DDNetworkManagerDate makeUrl_getuserinfo_with_token:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            NSDictionary *data = [responseObject objectForKey:kRequsetDataKey];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_id"]] forKey:@"user_id"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_nickname"]] forKey:@"user_nickname"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_portrait"]] forKey:@"user_headsmall"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_money"]] forKey:@"user_money"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_truename"]] forKey:@"user_truename"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_birthday"]] forKey:@"user_birthday"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_sex"]] forKey:@"user_sex"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"media_id"]] forKey:@"media_id"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"shop_id"]] forKey:@"shop_id"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_sign"]] forKey:@"user_sign"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_qrcode"]] forKey:@"user_qrcode"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_sn"]] forKey:@"user_sn"];

            [kUserDefaults synchronize];
        }
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
